package com.itheima.service.Impl;

import com.itheima.common.enums.ResponseCode;
import com.itheima.mapper.SysUserMapper;
import com.itheima.pojo.po.SysUser;
import com.itheima.pojo.vo.LoginReqVo;
import com.itheima.pojo.vo.LoginRespVo;
import com.itheima.pojo.vo.R;
import com.itheima.service.LoginService;
import com.itheima.utils.IdWorker;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private SysUserMapper mapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IdWorker idWorker;

    @Override
    public R<LoginRespVo> login(LoginReqVo vo) {


        if (vo == null || Strings.isEmpty(vo.getUsername()) || Strings.isEmpty(vo.getPassword()) || Strings.isEmpty
                (vo.getCode()) || Strings.isEmpty(vo.getRkey())) {
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }

        String rCode = (String) redisTemplate.opsForValue().get(vo.getRkey());
//        校验redis中和发送的验证码是否一致
        if (Strings.isEmpty(rCode) || !rCode.equals(vo.getCode())) {
//返回参数异常的信息
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }
        redisTemplate.delete(vo.getRkey());
//查询用户信息,根据传来的用户名查询密码,判断密码是否匹配
        SysUser user = mapper.findUserByUsername(vo.getUsername());
        if (user == null || !passwordEncoder.matches(vo.getPassword(),
                user.getPassword())) {
            return R.error(ResponseCode.SYSTEM_PASSWORD_ERROR.getMessage());
        }

        LoginRespVo respVo = new LoginRespVo();
        BeanUtils.copyProperties(user, respVo);
        return R.ok(respVo);
//        if (vo==null || Strings.isNullOrEmpty(vo.getUsername()) || Strings.isNullOrEmpty(vo.getPassword())){
//            return R.error(ResponseCode.DATA_ERROR.getMessage());
//        }
//        //根据用户名查询用户信息
//        SysUser user=this.sysUserMapper.findByUserName(vo.getUsername());
//        //判断用户是否存在，存在则密码校验比对
//        if (user==null || !passwordEncoder.matches(vo.getPassword(),user.getPassword())){
//            return R.error(ResponseCode.SYSTEM_PASSWORD_ERROR.getMessage());
//        }
//        //组装登录成功数据
//        LoginRespVo respVo = new LoginRespVo();
//        //属性名称与类型必须相同，否则copy不到
//        BeanUtils.copyProperties(user,respVo);
//        return  R.ok(respVo);
    }

    @Override
    public R<Map> GenerateCode() {
//
        String code = RandomStringUtils.randomNumeric(4);
        String rkey = String.valueOf(idWorker.nextId());
//        将键和值封装到redis中
        redisTemplate.opsForValue().set(rkey, code, 60, TimeUnit.SECONDS);
        HashMap<String, String> map = new HashMap<>();
        //将键和值返回到浏览器中
        map.put("rkey", rkey);
        map.put("code", code);
        return R.ok(map);

    }
}
